var $table = $("#branchcompanyTab");
var companyId;
$(function () {
	companyId = $.getUrlParam("id");
    initTable();
    
    //增加公司按钮
    $("#addBranchBut").click(function () {
    	branchCompanyEdit(companyId, 'add');
    });
    
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "get",	//使用get提交
        url: '../company/getBranchCompanyInfo.htm?companyId=' + companyId,
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "企业编号",
                field: "comnumNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "企业名称",
                field: "comnumName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "企业地址",
                field: "comnumAddress",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "企业联系方式",
                field: "comnumPhone",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "插入时间",
                field: "comnumInsTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "是否停运",
                field: "comnumIsDelete",
                align: 'center',
                formatter: function(value, row, index){
            		var stop = "";
            		if(row.comnumIsDelete == 1){
            			stop = '<span style="color:#ed5565;">已停运</span>';
            		}else if(row.comnumIsDelete == 0){
            			stop ='<span style="color:#23c6c8;">未停运</span>';
            		}
            		return stop;
            	}
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var e = '<button class="btn btn-xs btn-info" type="button" onclick="branchCompanyEdit(\'' + row.comnumId + '\',\'' + 'edit' + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
                    var d = '';
                    if (row.comnumIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="delBranchCompany(\'' + row.comnumId + '\',\'' + row.comnumName + '\',\'' + 1 + '\',\'' + row.comnumNum + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
                    }else if(row.comnumIsDelete == 1){
                        d = '<button class="btn btn-xs btn-warning" type="button" onclick="delBranchCompany(\'' + row.comnumId + '\',\'' + row.comnumName + '\',\'' + 0 + '\',\'' + row.comnumNum + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
                    }
                    return e + d;
                }
            }
        ]
    });
}


//编辑分公司信息，type是类型 add为增加，edit为编辑
function branchCompanyEdit(id, type){
	var title;
    if(id){
        title = '编辑分公司';
    }else{
        title = '新增分公司';
    }
    var index = layer.open({
        type: 2,
        title: title,
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        area : ['500px' , '750px'],
        content: '../company/edit_branch_company.htm?id='+id + '&type=' + type,
        success: function(layero, index){
        }
    });
    layer.full(index);
}

//设置公司状态
function delBranchCompany(companyId, companyName, isDelete, companyNum) {
    layer.confirm('你确定要修改【' + companyName + '】企业状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../company/modifyCompanyDisable.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'companyId': companyId,'isDelete':isDelete, 'companyNum':companyNum},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../company/getCompanyPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}